// 144. 二叉树的前序遍历

/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     val: number
 *     left: TreeNode | null
 *     right: TreeNode | null
 *     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.left = (left===undefined ? null : left)
 *         this.right = (right===undefined ? null : right)
 *     }
 * }
 */

// 前序遍历 根节点-》左节点-》右节点
function preorderTraversal(root: TreeNode | null): number[] {
  if (!root) return [];
  const start: TreeNode[] = [];
  const result: number[] = [];
  start.push(root);

  while (start.length) {
    const node: TreeNode = start.pop();
    result.push(node.val);
    if (node.right) {
      start.push(node.right);
    }
    if (node.left) {
      start.push(node.left);
    }
  }
}
